# balance table calculations with both sum and (mean)

meantab_both <- function (X_mat, t_ind, t_id, c_id, exact = NULL, digits = 2) 
{
        n_vrs = ncol(X_mat)
        n_obs = nrow(X_mat)
        output = matrix(NA, n_vrs, 8)
        rownames(output) = colnames(X_mat)
        colnames(output) = c("Mis", "Min", "Max", "Mean T", "Mean C", 
                             "Std Dif", "P-val", "Dev")
        for (j in 1:n_vrs) {
                n_miss = sum(is.na(X_mat[, j]))
                miss_pct = round(n_miss/n_obs, 2)
                if (n_miss < n_obs) {
                        yes = unlist(X_mat[t_id, j])
                        no = unlist(X_mat[c_id, j])
                        yes_before = unlist(X_mat[t_ind == 1, j])
                        no_before = unlist(X_mat[t_ind == 0, j])
                        
                        mean_yes = mean(yes, na.rm = TRUE)
                        mean_no = mean(no, na.rm = TRUE)
                        
                        min_val = min(c(yes, no), na.rm = TRUE)
                        max_val = max(c(yes, no), na.rm = TRUE)
                        pooled_sd = sqrt((var(yes_before, na.rm = TRUE) + 
                                                  var(no_before, na.rm = TRUE))/2)
                        std_diff = NA
                        p_val = NA
                        if (max_val > min_val) {
                                std_diff = (mean_yes - mean_no)/pooled_sd
                                if (is.null(exact)) {
                                        p_val = t.test(yes, no)$p.value
                                }
                                if (!is.null(exact)) {
                                        if (exact[j] == "w") {
                                                p_val = wilcox.test(yes, no)$p.value
                                        }
                                        if (exact[j] == "f") {
                                                tab_aux = table(t_ind[c(t_id, c_id)], unlist(X_mat[c(t_id, 
                                                                                                     c_id), j]))
                                                if (ncol(tab_aux) == 1) {
                                                        p_val = 1
                                                }
                                                if (ncol(tab_aux) != 1) {
                                                        p_val = fisher.test(tab_aux)$p.value
                                                }
                                        }
                                }
                        }
                        
                        #binary = 1
                        #upperlimit <- min(200, dim(X_mat)[1])
                        #for (i in 1:upperlimit){
                        #        if (!is.na(X_mat[i,j])) {
                        #                if (X_mat[i,j] != 0 & X_mat[i,j] != 1) {binary = 0}
                        #        }
                        #}
                        
                        #if (binary == 0){
                                mean_yes = mean(yes, na.rm = TRUE)
                                mean_no = mean(no, na.rm = TRUE)
                                sum_yes = sum(yes, na.rm = TRUE)
                                sum_no = sum(no, na.rm = TRUE)
                        #}
                        #else
                        #{
                                #mean_yes = length(which(yes == 1))
                                #mean_no = length(which(no == 1))
                        #        mean_yes = mean(yes, na.rm = TRUE)
                        #        mean_no = mean(no, na.rm = TRUE)
                        #        sum_yes = sum(yes, na.rm = TRUE)
                        #        sum_no = sum(no, na.rm = TRUE)
                        #}
                        
                        output[j, ] = c(round(miss_pct, digits), round(min_val,digits), round(max_val,digits), paste(round(sum_yes,digits)," (", round(mean_yes,digits),")", sep=""), 
                                        paste(round(sum_no,digits)," (", round(mean_no,digits),")", sep=""), round(std_diff,digits), round(p_val,digits), round(sum_yes - sum_no, digits))
                        
                }
        }
        #round(output, digits)
        return(output)
}